<template>
  <div class="hrt">
    <newHeader />
    <div class="w">
      <div class="box">
        <div class="a-box clearfix">
          <router-link to="/hrt/incomedetail" class="a fr">{{$t('hrt.t1')}}>></router-link>
        </div>
        <div class="num-box clearfix">
          <div class="item fl">
            <p class="num"><spinner v-if="loading.allStatistics"/><cu v-else :val="allStatisticsData.send_moeny | num" :decimals="0"></cu></p>
            <p class="name">{{$t('hrt.t2')}}</p>
          </div>
          <div class="item fr">
            <p class="num"><spinner v-if="loading.allStatistics"/><cu v-else  :val="allStatisticsData.all_moeny | num" :decimals="0"></cu></p>
            <p class="name">{{$t('hrt.t3')}}</p>
          </div>
        </div>
        <div class="slider">
          <div class="item1" :style="sliderStyle"></div>
          <div class="item2"></div>
        </div>
        <div class="item-box">
          <div class="item">
            <div class="num"><spinner v-if="loading.allStatistics"/><cu v-else  :val="allStatisticsData.yesterday | num" :decimals="0"></cu></div>
            <div class="name">{{$t('hrt.t4')}}</div>
          </div>
          <div class="item">
            <div class="num"><spinner v-if="id && loading.subStatistics"/><cu v-else-if="id"  :val="subStatisticsData.yesterday | num" :decimals="0"></cu><span v-else>-HRT</span></div>
            <div class="name">{{$t('hrt.t5')}}</div>
          </div>
          <div class="item">
            <div class="num"><spinner v-if="id && loading.subStatistics"/><cu v-else-if="id"  :val="subStatisticsData.sum | num" :decimals="0"></cu><span v-else>-HRT</span></div>
            <div class="name">{{$t('hrt.t6')}}</div>
          </div>
        </div>
        <div class="bottom-box">
          <button class="a" @click="addAddress" :disabled="loading.subStatistics">{{ subStatisticsData.address ? $t('subaccount.update_wallet') : $t('hrt.t7')}}>></button>
          <p>
            <router-link class="info" to="/hrt/howgetaddress">{{$t('hrt.t8')}}</router-link>
          </p>
        </div>
      </div>
      <div class="box">
        <div class="row">
          <div class="col">
            <h3 class="title">{{$t('hrt.t9')}}</h3>
            <p class="text">{{$t('hrt.t10')}}</p>
          </div>
          <div class="col">
            <h3 class="title">{{$t('hrt.t11')}}</h3>
            <p class="text">{{$t('hrt.t12')}}</p>
          </div>
        </div>
        <div class="a-box">
          <router-link class="a" to="/hrt/miningdetail">{{$t('hrt.t13')}}>></router-link>
        </div>
      </div>
    </div>
    <newFooter />
    <el-dialog :visible.sync="showAddAddressDialog" @close="$refs.form.resetFields();" top="25vh">
      <div class="title">{{ subStatisticsData.address ? $t('subaccount.update_wallet') : $t('wallet.address.address')}}</div>
      <div class="ct">
        <el-form :model="form" ref="form" :rules="rules">
          <div class="el-form-item" v-if="subStatisticsData.address">
            <label for="address" class="el-form-item__label">{{$t('hrt.t15')}}</label>
            <div class="el-form-item__content">
              <div class="el-input">
                <input type="text" v-model="subStatisticsData.address" autocomplete="off" class="el-input__inner" disabled>
              </div>
            </div>
          </div>
          <el-form-item prop="address" :label="$t('subaccount.new_address')">
            <el-input v-model="form.address" :placeholder="$t('hrt.t14')" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="code" class="has-btn" :label="$t('forget.input_code')">
            <el-input v-model="form.code" :placeholder="$t('forget.email_code_info')" autocomplete="off"></el-input>
            <el-button class="code-btn" type="primary" size="mini" @click="getcode" :disabled="!code.v_code_btn">{{code.codeStr}}</el-button>
          </el-form-item>
        </el-form>
        <div class="btn-box">
          <button class="btn" @click="submitForm" :disabled="loading.saveDoubleAddresss">{{$t('btn.confirm')}}</button>
        </div>
        <div class="history" v-if="address_log.length">
          <div class="h-t">
            <h5>{{$t('subaccount.update_history')}}</h5>
          </div>
          <div class="h-c">
            <ul>
              <li v-for="(v,k) in address_log" :key="k">
                <div class="clearfix li-top">
                  <span class="fl">{{$t('subaccount.update_time')}}：{{v.updated_at}}</span>
                  <span class="fr">{{$t('subaccount.update_ip')}}：{{v.ip}}</span>
                </div>
                <div class="li-bottom">
                  <p>{{$t('subaccount.address')}}：<input readonly :value="v.oldaddress"/></p>
                  <p>{{$t('subaccount.new_address')}}：<input readonly :value="v.newaddress"/></p>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import newHeader from '@/components/Header';
import newFooter from '@/components/Footer';
import { mapGetters } from 'vuex';
import { saveDoubleAddresss, getcode, sendTelCode, allStatistics, subStatistics, updateDoubleAddressLog } from '@/api';
import cu from '@/components/CountUp';
import spinner from '@/components/Spinner';

export default {
  name: 'index',
  components: {
    newHeader,
    newFooter,
    cu,
    spinner,
  },
  data() {
    return {
      coin_type: 'hrt',
      loading: {
        saveDoubleAddresss: false,
        allStatistics: false,
        subStatistics: false,
        updateDoubleAddressLog: false,
      },
      allStatisticsData: {
        all_moeny: 0,
        send_moeny: 0,
        yesterday: 0,
      },
      subStatisticsData: {
        sum: 0,
        yesterday: 0,
        address: '',
      },
      address_log: [],
      showAddAddressDialog: false,
      form: {
        address: '',
        code: '',
      },
      rules: {
        address: [
          { required: true, message: this.$t('subaccount.input_wallet_address') },
        ],
        code: [
          {
            required: true,
            message: this.$t('forget.email_code_info'),
            trigger: 'change',
          },
        ],
      },
      code: {
        v_code_btn: true,
        codeStr: this.$t('btn.sendcode.getcode'),
        time: 60,
        timer: null,
      },
    };
  },
  computed: {
    ...mapGetters(['language', 'subCuid', 'email', 'loginType', 'tel', 'id']),
    sliderStyle() {
      return {
        width: `${(this.all_moeny ? this.send_moeny / this.all_moeny : 0) * 1012}px`,
      };
    },
  },
  created() {
    this.allStatistics();
    if (this.id) {
      this.subStatistics();
      this.updateDoubleAddressLog();
    }
  },
  methods: {
    addAddress() {
      if (this.id) {
        this.showAddAddressDialog = true;
      } else {
        this.$router.push('/login?from_url=%2Fhrt%2Fhome');
      }
    },
    updateDoubleAddressLog() {
      this.loading.updateDoubleAddressLog = true;
      updateDoubleAddressLog({
        puid: this.subCuid,
        coin_type: this.coin_type,
      }).then((res) => {
        if (res.data.status === 1) {
          this.address_log = res.data.data;
        }
        this.loading.updateDoubleAddressLog = false;
      }).catch(() => {
        this.loading.updateDoubleAddressLog = false;
      });
    },
    subStatistics() {
      this.loading.subStatistics = true;
      subStatistics({ puid: this.subCuid, coin_type: this.coin_type }).then((res) => {
        this.loading.subStatistics = false;
        if (res.data.status === 1) {
          const data = res.data.data;
          [this.subStatisticsData.sum, this.subStatisticsData.yesterday, this.subStatisticsData.address] = [data.sum || 0, data.yesterday || 0, data.address || ''];
        } else {
          this.$message.error(this.$t(`api.${res.data.errcode}`));
        }
      }).catch(() => {
        this.loading.subStatistics = false;
      });
    },
    allStatistics() {
      this.loading.allStatistics = true;
      allStatistics().then((res) => {
        this.loading.allStatistics = false;
        if (res.data.status === 1) {
          const data = res.data.data;
          [this.allStatisticsData.all_moeny, this.allStatisticsData.send_moeny, this.allStatisticsData.yesterday] = [data.all_moeny || 0, data.send_moeny || 0, data.yesterday || 0];
        } else {
          this.$message.error(this.$t(`api.${res.data.errcode}`));
        }
      }).catch(() => {
        this.loading.allStatistics = false;
      });
    },
    getcode() {
      this.code.v_code_btn = false;
      this.code.codeStr = `${this.$t('btn.sendcode.resend')}(60s)`;
      clearInterval(this.code.timer);
      this.code.timer = setInterval(() => {
        if (this.code.time > 0) {
          this.code.time -= 1;
          this.code.codeStr = `${this.$t('btn.sendcode.resend')}(${this.code.time}s)`;
        } else {
          clearInterval(this.code.timer);
          this.code.time = 60;
          this.code.codeStr = this.$t('btn.sendcode.sendcode');
          this.code.v_code_btn = true;
        }
      }, 1000);
      if (this.loginType === 'email') {
        getcode({
          email: this.email,
          type: 'set_double_addresss',
        }).then((res) => {
          if (res.data.status === 1) {
            this.$notify({
              message: this.$t('set.send_email_code'),
              type: 'success',
              duration: 2000,
              showClose: false,
            });
          } else {
            this.$message({
              message: this.$t(`api.${res.data.errcode}`),
              type: 'warning',
            });
          }
        }).catch(() => {
          this.$message.error(this.$t('error.catchError'));
        });
      } else {
        sendTelCode({
          tel: this.tel,
          type: 'set_double_addresss',
        }).then((res) => {
          if (res.data.status === '1') {
            this.$notify({
              message: this.$t('set.send_phone_code'),
              type: 'success',
              duration: 2000,
              showClose: false,
            });
          } else {
            this.$message({
              message: this.$t(`api.${res.data.errcode}`),
              type: 'warning',
            });
          }
        }).catch(() => {
          this.$message.error(this.$t('error.catchError'));
        });
      }
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.loading.saveDoubleAddresss = true;
        saveDoubleAddresss(Object.assign({ puid: this.subCuid, coin_type: this.coin_type }, this.form)).then((res) => {
          this.loading.saveDoubleAddresss = false;
          if (res.data.status === 1) {
            this.showAddAddressDialog = false;
            this.$notify({
              message: this.$t('api.6086'),
              type: 'success',
              duration: 2000,
              showClose: false,
            });
            this.subStatisticsData.address = this.form.address;
            this.updateDoubleAddressLog();
          } else {
            this.$message.error(this.$t(`api.${res.data.errcode}`));
          }
        }).catch(() => {
          this.$message.error(this.$t('error.catchError'));
          this.loading.saveDoubleAddresss = false;
        });
      });
    },
  },
  watch: {
    language() {
      this.rules.address[0].message = this.$t('subaccount.input_wallet_address');
      this.rules.code[0].message = this.$t('forget.email_code_info');
    },
  },
};
</script>
<style lang="scss" scoped>
.hrt{
  .w{
    padding: 40px 0;
    .box{
      padding-left: 100px;
      padding-right: 86px;
      background-color: #ffffff;
      box-shadow: 0px 0px 5px 0px
        rgba(0, 0, 0, 0.05);
      border-radius: 3px;
      border: solid 1px #e0e0e0;
      &:nth-of-type(1){
        .a-box{
          padding: 50px 0 59px;
          a{
            color: #565656;
            font-size: 16px;
          }
        }
        .num-box{
          .num{
            font-size: 30px;
            color: #70c1b3;
            line-height: 45px;
          }
          .name{
            margin-top: 6px;
            text-align: center;
            color: #cccccc;
            font-size: 18px;
            line-height: 27px;
          }
        }
        .slider{
          margin-top: 60px;
          height: 86px;
          display: flex;
          padding-bottom: 60px;
          background: url(../../assets/images/hrt/slider-bg.png) center bottom no-repeat;
          .item1,.item2{
            height: 100%;
          }
          .item1{
            background-image: linear-gradient(-90deg,#4ed3d5 0%,#42cdd2 48%,#247ba0 100%),linear-gradient(#247ba0,#247ba0);
            transition: ease-in-out 1s width;
          }
          .item2{
            flex: 1;
            background-image: linear-gradient(90deg,#c8dfe5, #f4f5f5);
          }
        }
        .item-box{
          display: flex;
          margin-top: 112px;
          justify-content: space-between;
          .item{
            padding-left: 57px;
            position: relative;
            &::before{
              position: absolute;
              top: 0;
              left: 0;
              width: 40px;
              height: 40px;
              content: ' ';
              display: block;
              border-radius: 50%;
              background-image: url(../../assets/images/wallet-icon1.png);
              background-size: 24px 25px;
              background-position: center center;
              background-repeat: no-repeat;
            }
            @each $index,$color in (1:#56b5c8,2:#f7cc86,3:#96c6f6) {
              &:nth-of-type(#{$index}) {
                &::before{
                  background-color: #{$color}
                }
              }
            }
            .num{
              color: #323232;
              font-size: 30px;
              line-height: 25px;
            }
            .name{
              color: #cccccc;
              font-size: 14px;
              line-height: 16px;
            }
          }
        }
        .bottom-box{
          padding: 85px 0 65px;
          text-align: right;
          .a{
            display: inline-block;
            outline: none;
            border: none;
            width: 135px;
            height: 30px;
            background-color: #70c1b3;
            border-radius: 3px;
            line-height: 30px;
            font-size: 16px;
            color: #fff;
            transition: background-color 0.1s ease-in-out;
            &:hover {
              background-color: #8dcdc2;
            }
            &:disabled {
              background-color: #8dcdc2;
              cursor: not-allowed;
            }
          }
          .info{
            display: inline-block;
            margin-top: 15px;
            line-height: 22px;
            color: #e4377f;
            font-size: 14px;
          }
        }
      }
      &:nth-of-type(2){
        margin-top: 20px;
        padding-top: 90px;
        padding-bottom: 98px;
        .row{
          display: flex;
          margin-top: 36px;
          &:nth-of-type(1){
            margin-top: 0;
          }
          .col{
            position: relative;
            flex: 1;
            margin-right: 130px;
            &:nth-of-type(even){
              margin-right: 0;
            }
            .title,.text{
              padding-left: 54px;
              position: relative;
              &::before{
                position: absolute;
                top: 0;
                left: 0;
                display: block;
                width: 36px;
                height: 36px;
                background-color: #217899;
                border-radius: 12px;
                font-size: 24px;
                color: #fff;
                line-height: 36px;
                text-align: center;
              }
            }
            .title{
              font-size: 18px;
              color: #70c1b3;
              line-height: 36px;
              &::before{
                content: 'Q';
                background-color: #70c1b3;
              }
            }
            .text{
              padding-top: 4px;
              margin-top: 20px;
              font-size: 14px;
              color: #323232;
              line-height: 20px;
              &::before{
                content: 'A';
                background-color: #217899;
              }
            }
          }
        }
        .a-box{
          text-align: right;
          margin-top: 210px;
          .a{
            color: #70c1b3;
            font-size: 20px;
          }
        }
      }
    }
  }
  /deep/ .el-dialog{
    width: 448px;
    background-color: #ffffff;
    box-shadow: 0px 0px 5px 0px
      rgba(0, 0, 0, 0.05);
    border-radius: 5px;
    border: solid 1px #e0e0e0;
    .el-dialog__header{
      height: 0;
      padding: 0;
      .el-dialog__headerbtn{
        top: -50px;
        right: 0;
        border-radius: 15px;
        width: 30px;
        height: 30px;
        border: 1px solid #a6a6a6;
        transition: background-color 0.1s ease-in-out;
        .el-dialog__close{
          color: #a6a6a6;
        }
        &:hover{
          border-color: #fff;
          .el-dialog__close{
            color: #fff;
          }
        }
      }
    }
    .el-dialog__body{
      padding: 0;
    }
    .el-form{
      .el-form-item{
        margin-top: 36px;
        &:first-of-type{
          margin-top: 0;
        }
        &.has-btn{
          position: relative;
          .el-input__inner{
            padding-right: 96px;
          }
          .code-btn{
            position: absolute;
            right: 3px;
            bottom: 7px;
          }
        }
      }
      .el-form-item__label{
        line-height: 22px;
        font-size: 14px;
        color: #919191;
        &::before{
          display: none;
        }
      }
      .el-input__inner{
        border-top: none;
        border-right: none;
        border-left: none;
        border-radius: 0;
        padding: 0;
      }
    }

    .title{
      height: 50px;
      line-height: 50px;
      background-color: #f9f9f9;
      border-radius: 5px 5px 0px 0px;
      color: #333333;
      font-size: 16px;
      padding: 0 38px;
    }
    .ct{
      padding: 50px 40px 25px;
      .btn-box{
        text-align: center;
        margin-top: 60px;
        .btn{
          outline: none;
          border: none;
          width: 120px;
          height: 30px;
          background-color: #70c1b3;
          box-shadow: 0px 4px 12px 0px
            rgba(0, 130, 107, 0.2);
          border-radius: 4px;
          font-size: 14px;
          color: #fff;
          transition: background-color 0.1s ease-in-out;
          &:disabled {
            background-color: #8dcdc2;
            cursor: not-allowed;
          }
          &:hover {
            background-color: #8dcdc2;
          }
        }
      }
      .history{
        margin-top: 84px;
        .h-t{
          border-bottom: 1px solid #e8e8e8;
          h5{
            font-size: 14px;
            color: #999999;
            line-height: 34px;
          }
        }
        .h-c{
          height: 250px;
          overflow-y: auto;
          border-bottom: 1px solid #e8e8e8;
          span,p{
            padding-top: 15px;
            line-height: 22px;
            font-size: 14px;
            color: #6c6c6c;
          }
          li{
            border-top: 1px solid #e8e8e8;
            padding-bottom: 15px;
            &:first-of-type{
              border-top: none;
            }
          }
          .li-bottom{
            padding-left: 15px;
            p{
              display: flex;
              input{
                flex: 1;
                font-size: 14px;
                color: #6c6c6c;
                border: none;
                outline: none;
              }
            }
          }
        }
      }
    }
  }
}
</style>
